package frsf.cidisi.exercise.proyectoia.situationcalculus;

import frsf.cidisi.faia.agent.ActionFactory;
import frsf.cidisi.faia.agent.Perception;
import frsf.cidisi.faia.agent.situationcalculus.KnowledgeBase;
import frsf.cidisi.faia.agent.situationcalculus.SituationCalculusPerception;
import frsf.cidisi.faia.exceptions.PrologConnectorException;

import java.util.Hashtable;

public class StateDrone extends KnowledgeBase {

    public StateDrone() throws PrologConnectorException {

        super("drone_logic.pl");

        this.initState();
    }

    @Override
    public void updateState(Perception p) {
        this.tell((SituationCalculusPerception) p);
    }
    
    @Override
    public String toString() {
        String str = "";

        str = str + "\nPosicion=\"(" + this.getRow() + "," + "" + this.getColumn() + ")\"";
        
        str = str + "\nMundo=\"[ \n";
        for (int row = 0; row < this.getWorldSize(); row++) {
            str = str + "[ ";
            for (int col = 0; col < this.getWorldSize(); col++) {

                if (this.getRow() == row && this.getColumn() == col) {
                    str = str + "A" + " ";
                } else {
                    str = str + this.convertCell(this.getPositionState(row, col));
                }
            }
            str = str + "]\n";
        }
        str = str + " ]\"";
        
        return str;
        /*
        str = str + " position=\"(" + this.getRow() + "," + "" + this.getColumn() + ")\"";
        str = str + " energy=\"" + this.getEnergy() + "\"\n";

        str = str + "world=\"[ \n";
        for (int row = 0; row < this.getWorldSize(); row++) {
            str = str + "[ ";
            for (int col = 0; col < this.getWorldSize(); col++) {

                if (this.getRow() == row && this.getColumn() == col) {
                    str = str + "P" + " ";
                } else {
                    str = str + this.convertCell(this.getPositionState(row, col));
                }
            }
            str = str + " ]\n";
        }
        str = str + " ]\"";

        return str;*/
    }
    
    @Override
    public ActionFactory getActionFactory() {
        return DroneActionFactory.getInstance();
    }

    @Override
    public String getBestActionPredicate() {
        return "bestAction";
    }
    
    @Override
    public String getExecutedActionPredicate() {
        return "accionEjecutada";
    }
    
    @Override
    public String getSituationPredicate() {
        return "situacionActual";
    }

    @Override
    public void initState() {
       
    	
    	// Escenario 1
    	//this.addKnowledge("posicion(0,0,0)");
    	//this.addKnowledge("esquinaVisitada(0,0,0)");
        
        // Escenario 2
    	//this.addKnowledge("posicion(0,2,0)");
    	//this.addKnowledge("esquinaVisitada(0,2,0)");
    	
    	// Escenario 3
    	this.addKnowledge("posicion(1,1,0)");
    	this.addKnowledge("esquinaVisitada(1,1,0)");
    }

 // These methods are internal:

    private int[] getPosition() {
        String positionQuery = "posicion(X,Y," + this.getSituation() + ")";

        Hashtable[] result = this.query(positionQuery);

        int x = Integer.parseInt(result[0].get("X").toString());
        int y = Integer.parseInt(result[0].get("Y").toString());

        return new int[]{x, y};
    }

    private int getRow() {
        return this.getPosition()[0];
    }

    private int getColumn() {
        return this.getPosition()[1];
    }
    
    private int getWorldSize() {
        return 3;
    }

    private String convertCell(int p) {
        String r = "";

        if (p == DronePerception.DESCONOCIDO) {
            r = "?";
        } else if (p == DronePerception.PERSONAS) {
            r = "P";
        } else if (p == DronePerception.VICTIMARIO) {
            r = "V";
        } else if (p == DronePerception.ESQUINA_VISTA) {
        	r = "S";
        }
        

        return r + " ";
    }

    private int getPositionState(int row, int col) {

        // Query the cell state to see if we know it
        String cellStateQuery = "celdaConocida(" + row + "," +
                col + "," + this.getSituation() + ")";

        if (!this.queryHasSolution(cellStateQuery)) {
            return DronePerception.DESCONOCIDO;
        }

        // Query the cell state to see if there is an enemy there
        cellStateQuery = "victimario(" + row + "," +
                col + "," + this.getSituation() + ")";

        if (this.queryHasSolution(cellStateQuery)) {
            return DronePerception.VICTIMARIO;
        }

        // Query the cell state to see if there is food there
        cellStateQuery = "personas(" + row + "," +
                col + "," + this.getSituation() + ")";

        if (this.queryHasSolution(cellStateQuery)) {
            return DronePerception.PERSONAS;
        }

        // Query the cell state to see if it is empty
        cellStateQuery = "esquinaVista(" + row + "," +
                col + "," + this.getSituation() + ")";

        if (this.queryHasSolution(cellStateQuery)) {
            return DronePerception.ESQUINA_VISTA;
        }

        return -100;
    }
    
    
}
